var carRealInfo = [];
var orderRealInfo = [];
var forTimer = 10000;
init();
function init(){
     AMapLoader.load({ //首次调用 load
        key:'4320b0255e963ddc50c3787e73b0e91a',
        version:'2.0',
        plugins:['AMap.Scale','AMap.ToolBar','AMap.MapType', 'AMap.MouseTool']
    }).then((AMap)=>{
        map = new AMap.Map('container', {
            center: [ 106.233383,38.490073],
            zoom: 12,
            dragEnable: true
        });
        map.addControl(new AMap.Scale())
        map.addControl(new AMap.ToolBar())
        map.addControl(new AMap.MapType())
        
        // 点标记
        var marker = new AMap.Marker({
            position: new AMap.LngLat(106.222829, 38.44066),
            title: '银川IBI育成中心',
            zIndex: 120
        })
        marker.on('click', openInfo);
        map.add(marker);
        
        function openInfo(e) {
            var info = [];
            info.push("<div><div style=\"padding:0px 0px 0px 4px;\"><b>银川方达电子系统工程有限公司</b>");
            info.push("电话 : 0951-4679186   邮编 : 750001");
            info.push("地址 :银川市金凤区宁安南街IBI育成中心一期六号楼</div></div>");
            infoWindow = new AMap.InfoWindow({
                content: info.join("<br/>")
            });
            infoWindow.open(map, [e.lnglat.lng, e.lnglat.lat]);
        }

        // 创建多边形覆盖物实例
        var path = [
            new AMap.LngLat(106.218659,38.442327),
            new AMap.LngLat(106.224639,38.441814),
            new AMap.LngLat(106.224302,38.439065),
            new AMap.LngLat(106.218292,38.439625)
        ];
        var polygon = new AMap.Polygon({
            path: path,  
            strokeColor: "#F33",  //线颜色
            strokeOpacity: 1,  //线透明度
            strokeWeight: 2,  //线粗细度
            fillColor: "#ee2200",  //填充颜色
            fillOpacity: 0.35 //填充透明度
        });
        map.add(polygon);
        
        //绘制覆盖物
        var overlays = [];
        var mouseTool = new AMap.MouseTool(map); 
        mouseTool.on('draw',function(e){
            overlays.push(e.obj);
        })
        function draw(type){
          overlays = []
          switch(type){
            case 'marker':{
                mouseTool.marker({
                });
                break;
            }
            case 'polyline':{
                mouseTool.polyline({
                  strokeColor:'#80d8ff'
                });
                break;
            }
            case 'polygon':{
                mouseTool.polygon({
                  fillColor:'#00b0ff',
                  strokeColor:'#80d8ff'
                });
                break;
            }
            case 'rectangle':{
                mouseTool.rectangle({
                  fillColor:'#00b0ff',
                  strokeColor:'#80d8ff'
                });
                break;
            }
            case 'circle':{
                mouseTool.circle({
                  fillColor:'#00b0ff',
                  strokeColor:'#80d8ff'
                });
                break;
            }
          }
        }
        var radios = document.getElementsByName('func');
        for(var i=0;i<radios.length;i+=1){
            radios[i].onchange = function(e){
              draw(e.target.value)
            }
        }
        
        document.getElementById('clear').onclick = function(){
            map.remove(overlays)
            overlays = [];
        }
        
        document.getElementById('save').onclick = function(){
            map.remove(overlays)
            console.log(overlays)
        }
        
        document.getElementById('close').onclick = function(){
            mouseTool.close(true)
            for(var i=0;i<radios.length;i+=1){
                radios[i].checked = false;
            }
        }
        
        // 自动适配到合适视野范围
        // 无参数，默认包括所有覆盖物的情况
        // map.setFitView();
     
        // Amap地图点击事件
        map.on('click', function(ev) {
            var text = '您在 [ '+ev.lnglat.getLng()+','+ev.lnglat.getLat()+' ] 的位置单击了地图！';
            $("#text").text(text);
        });
    }).catch((e)=>{
        console.error(e);
    });
}